define(function(){
	var TypeWindow = Ext.extend(Ext.Window, {
		modifiedId: null, //从构造器中传入，表示要编辑的对象的id，为空表示新增
		parentType: null, //在构造器中传入，表示要编辑对象的父节点
		resizable: false,
		modal: true,
		width: 350,
	    animateTarget: cu.getDefaultAnimateTarget(),
        bodyStyle:'padding:3px;',
		iconCls: 'icon-window',
		initComponent: function(){
			TypeWindow.superclass.initComponent.call(Ext.apply(this, {
				title: this.modifiedId ? '修改FQA类型' : '添加FQA类型',
				items: [this.formPanel = this.createFormPanel()],
				buttons: [
				    {text: '保存', width:60, handler: this.save, iconCls: 'icon-save', scope: this},
				    {text: '关闭', width:60, handler: this.close, iconCls: 'icon-close', scope: this}
				],
				listeners: {
					show: function(){
						this.modifiedId && this.initFormData();
						this.getForm().firstFocus();
						cu.bindEnter(this, this.save);
					}
				}
			}));
		},
		save: function(){
			cu.submit({
				form: this.getForm(), 
				url: 'misc/fqa/saveType.do',
				success: function(data){
					if (this.callback){
						var parentNode = this.getField('parentId').getValueNode();
						this.callback.call(this.cbScope, data, parentNode.getPath());
					}
					this.close();
				},
				scope: this
			});
		},
		initFormData: function(){
			cu.get({
				url:'misc/fqa/getType.do',
				params: {id: this.modifiedId},
				success: function(data){
					this.getForm().setValues(data);
				},
				scope: this
			});
		},
		createFormPanel: function(){
			var formPanel = new Ext.form.FormPanel({
			    baseCls: 'x-plain',
			    bodyStyle:'padding:5px;',
			    labelWidth: 56,
			    labelPad: 5,
			    labelAlign: "right",
			    defaults: {anchor:'99%', style:'margin-bottom:5px;'},
			    items: [
			        {xtype:'hidden', name:'id'},
			        {
						xtype:'combotree',
						fieldLabel:'父类型',
						hiddenName: 'parentId',
						value: this.modifiedId ? null : this.parentType.id,
						height: 200,
						dataUrl: 'misc/fqa/getTypeChildren.do',
						nodePathUrl: 'sys/util/getEntityAttrPath.do?c=misc.FqaType',
						root: {id:'0', text:'根类型', expanded: true},
						rootVisible: true,
						rootSelectable: true,
						rootValue: 0,
						showFullPath: true,
						allowBlank: false,
				        validator: function(value){
				        	var selfId = this.ownerCt.getForm().findField('id').getValue();
				        	var parentId = this.ownerCt.getForm().findField('parentId').getValue();
				        	return parentId !== selfId ? true : "父类型不能是自身";
				        }
			        },{
						xtype:'textfield',
						name: 'name',
						fieldLabel: '类型名称',
						maxLength: 30, maxLengthText: '类型名称不能超过30个字',
						allowBlank:false
					}
			    ],
			    firstFocus: function(){
			    	this.findField('name').focus(false, 200);
			    }
			});
			return formPanel;
		},
		getForm: function(){
			return this.formPanel.getForm();
		},
		getField: function(name){
			return this.getForm().findField(name);
		}
	});
	return TypeWindow;
});